package com.iwhalecloud.bss.kite.cucc.order.loader;

import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.cucc.order.loader.impl.ProdInstLoader;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.IInst;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 实例加载器工厂
 */
public abstract class InstLoaderFactory {

    /**
     * 表名和实例加载器映射
     */
    private static Map<String, Class<? extends IInstLoader<? extends IInst>>> instLoaderMap = new HashMap<>();

    static {
        instLoaderMap.put(TableConsts.ProdInst.getTableName().toUpperCase(), ProdInstLoader.class);
    }

    /**
     * 获取实例加载器
     * @param tableName
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <I extends IInst, T extends IInstLoader<I>> T getInstLoader(String tableName) {
        if (StringUtils.isBlank(tableName)) {
            return (T) SpringUtil.getBean("orderInstLoader");
        }

        Class<? extends IInstLoader<? extends IInst>> instLoaderClass = instLoaderMap.get(tableName.toUpperCase());
        if (Objects.nonNull(instLoaderClass)) {
            return (T) SpringUtil.getBean(instLoaderClass);
        }
        else {
            return (T) SpringUtil.getBean("orderInstLoader");
        }
    }
}
